package com.chushouya.manager.service.api.impl;


import com.chushouya.common.support.Contexts;
import com.chushouya.manager.constants.BonusStatusEnum;
import com.chushouya.manager.constants.BonusTypeEnum;
import com.chushouya.manager.dao.entity.BonusUserEntity;
import com.chushouya.manager.dao.repository.BonusUserRepository;
import com.chushouya.manager.dto.admin.bonus.BonusQuery;
import com.chushouya.manager.dto.admin.bonus.CanUseBonusQuery;
import com.chushouya.manager.dto.admin.bonus.UserExchangeDTO;
import com.chushouya.manager.dto.api.bonus.CanUserBonusDTO;
import com.chushouya.manager.service.api.UserBonusApiService;
import com.general.framework.core.lang.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserBonusApiServiceImpl implements UserBonusApiService {


    @Resource
    private BonusUserRepository userBonusRepository;

//    @Resource
//    private BonusTypeApiService bonusTypeApiService;


    @Override
    public List<CanUserBonusDTO> userBonusList(BonusQuery query) {
        final String appId = Contexts.getHeaderContext().getAppId();
        final Long userId = Contexts.getUserContext().getUserId();
        query.setAppId(null);
        query.setUserId(userId);
        log.info("查询用户红包列表，参数：{}", Jsons.toJsonString(query));
        List<BonusUserEntity> userBonusEntityList = userBonusRepository.selectList(query);
        List<CanUserBonusDTO> userBonusDTOList = new ArrayList<>();
        userBonusEntityList.forEach(
                userBonusEntity -> {
                    CanUserBonusDTO userBonusDTO = Beans.copy(userBonusEntity, CanUserBonusDTO.class);
                    userBonusDTO.setBonusName(userBonusEntity.getTypeName());
                    userBonusDTO.setBonusValue(userBonusEntity.getTypeMoney());
                    userBonusDTOList.add(userBonusDTO);
                }
        );
        return userBonusDTOList;
    }

    @Override
    public List<CanUserBonusDTO> canUseBonusQuery(CanUseBonusQuery query) {
        final BigDecimal evaluatePrice = query.getAmount();
        final Long userId = Contexts.getUserContext().getUserId();
        BonusQuery bonusQuery = new BonusQuery();
        bonusQuery.setBonusStatus(BonusStatusEnum.UNUSED.value());
        bonusQuery.setAmount(evaluatePrice);
        bonusQuery.setBonusType(BonusTypeEnum.RETRIEVE.value());
        bonusQuery.setUserId(userId);
        List<BonusUserEntity> userBonusEntityList = userBonusRepository.selectList(bonusQuery);
        List<CanUserBonusDTO> userBonusDTOList = new ArrayList<>();
        userBonusEntityList.forEach(
                userBonusEntity -> {
                    CanUserBonusDTO userBonusDTO = Beans.copy(userBonusEntity, CanUserBonusDTO.class);
                    userBonusDTO.setBonusName(userBonusEntity.getTypeName());
                    userBonusDTO.setBonusValue(userBonusEntity.getTypeMoney());
                    userBonusDTOList.add(userBonusDTO);
                }
        );
        return userBonusDTOList;

    }

    @Override
    public void exchange(UserExchangeDTO dto) {
        final String ticketNo = dto.getTicketNo();
        final Long userId = Contexts.getUserContext().getUserId();
        final String appId = Contexts.getHeaderContext().getAppId();
        BonusUserEntity userBonusEntity = userBonusRepository.selectFirstByTicketNo(ticketNo);
        Validate.isNull(userBonusEntity, "券码不存在");
//        final String typeIds = userBonusEntity.getTypeIds();
//        List<Long> typeIdList = Jsons.parseArray(typeIds, Long.class);
//        Validate.isEmpty(typeIdList, "券包不含券码");
        Validate.isNotNull(userBonusEntity.getUserId(), "券码已被领取");
        Validate.isFalse(Longs.isNullOrZero(userBonusEntity.getOrderId()), "券码已使用");

        BonusUserEntity updateEntity = new BonusUserEntity();
        updateEntity.setBonusId(userBonusEntity.getBonusId());
        updateEntity.setUserId(userId);
        updateEntity.setEndTime(Dates.getTimeNow());
        updateEntity.setUseTime(Dates.getTimeNow());
        updateEntity.setAppId(appId);

        userBonusRepository.updateByPrimaryKeySelective(updateEntity);

        //领券
//        bonusTypeApiService.userReceiveBonus(typeIdList);

    }



}
